import { ConfigType, registerAs } from '@nestjs/config'

import dotenv from 'dotenv'

import { DataSource, DataSourceOptions } from 'typeorm'

import { env, envBoolean, envNumber } from '~/global/env'

dotenv.config({ path: `.env.${process.env.NODE_ENV}` })

const dataSourceOptions: DataSourceOptions = {
  type: 'postgres',
  host: env('DB_READER_HOST', '127.0.0.1'),
  port: envNumber('DB_READER_PORT', 3306),
  username: env('DB_USERNAME'),
  password: env('DB_PASSWORD'),
  database: env('DB_DATABASE'),
  synchronize: envBoolean('DB_SYNCHRONIZE', false),
  entities: ['dist/modules/**/*.entity{.ts,.js}'],
  migrations: ['dist/migrations/*{.ts,.js}'],
  subscribers: ['dist/modules/**/*.subscriber{.ts,.js}'],
  logging: true,
}
export const dbReadRegToken = 'database_read'

export const DatabaseReadConfig = registerAs(
  dbReadRegToken,
  (): DataSourceOptions => dataSourceOptions,
)

export type IDatabaseReadConfig = ConfigType<typeof DatabaseReadConfig>

const dataSource = new DataSource(dataSourceOptions)

export default dataSource